package org.edu.modules.jxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.edu.common.system.base.service.impl.BaseServiceImpl;
import org.edu.modules.jxzy.entity.JwKecheng;
import org.edu.modules.jxzy.mapper.JwKechengMapper;
import org.edu.modules.jxzy.service.IJwKechengService;
import org.edu.modules.scheduleCommon.dto.BasePageDto;
import org.edu.modules.scheduleCommon.dto.KechengStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 教务课程信息
 * @Author: jeecg-boot
 * @Date: 2021-04-15
 * @Version: V1.0
 */
@Service
public class JwKechengServiceImpl extends ServiceImpl<JwKechengMapper, JwKecheng> implements IJwKechengService {

    @Autowired
    private JwKechengMapper jwKechengMapper;

    @Override
    public IPage<JwKecheng> pageDto(BasePageDto page, Wrapper queryWrapper) {
        return baseMapper.pageDto(page, queryWrapper);
    }

    @Override
    public void updateStatusByIds(String ids, int status) {
        List<String> list = Arrays.asList(ids.split(","));
        for (String str : list) {
            baseMapper.updateById(new JwKecheng().setId(Integer.parseInt(str)).setStatus(status));
        }
    }

    @Override
    public int queryCount() {
        LambdaQueryWrapper<JwKecheng> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JwKecheng::getIsdelete, 0);
        return this.count(wrapper);
    }

    @Override
    public int queryCountByZyzIdAndKcxz(Integer zyzId, Integer kcxz) {
        LambdaQueryWrapper<JwKecheng> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JwKecheng::getFalid, zyzId);
        if (kcxz != null) {
            wrapper.eq(JwKecheng::getKcxz, kcxz);
        }
        wrapper.eq(JwKecheng::getIsdelete, 0);
        return this.count(wrapper);
    }

    @Override
    public List<KechengStatistics> kechengStatistics(Integer falid, Integer kcxz) {
        List<KechengStatistics> kechengStatisticsList = jwKechengMapper.kechengStatistics(falid, kcxz);
        return kechengStatisticsList;
    }

    @Override
    public int importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            JwKecheng dto = new JwKecheng();
//                            dto.setStuId ((Integer) map.get("学生号"));
            dto.setKch((String) map.get("课程号（代码）"));
            dto.setKcmc((String) map.get("课程名称"));
            dto.setKcywm((String) map.get("课程英文名"));
            dto.setKcbm((String) map.get("课程别名"));
            dto.setKcjstidText((String) map.get("课程介绍"));
            dto.setFalid(19);//开课院系
            dto.setSpecid(118);//开课专业
//            dto.setJyzid ((String)map.get("所属教研组"));
            dto.setXf(BigDecimal.valueOf(Long.parseLong(map.get("学分") + "")));
            dto.setZxs(Integer.parseInt(map.get("总学时") + ""));
            this.save(dto);
        }
        System.out.println("mapList:" + mapList);
//        // 创建 ObjectMapper 实例
//        ObjectMapper objectMapper = new ObjectMapper();
//
//        for (Map map: mapList) {
//            // 将 Map 转换为 JwKecheng 对象
//            JwKecheng jwKecheng = objectMapper.convertValue(map, JwKecheng.class);
//            this.save(jwKecheng);
//
//        }
        return 0;
    }
}
